//
//  TopicTableViewCell.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/24.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "TopicTableViewCell.h"
#import "baseLabel.h"
#import "baseImageView.h"
#import <Masonry.h>
#import  <UIImageView+WebCache.h>


@interface TopicTableViewCell ()

@property (nonatomic,retain)baseLabel *labelTitle;//标题
@property (nonatomic,retain)baseImageView *imgPhoto;//图片
@property (nonatomic,retain)baseLabel *labelName;//用户名
@property (nonatomic,retain)baseLabel *labelTime;//时间
@property (nonatomic,retain)baseImageView *imgComment;//评论
@property (nonatomic,retain)baseLabel *labelComment;//评论数

@end


@implementation TopicTableViewCell

#pragma mark - ------------ 内存管理 ---------------

-(void)dealloc{
  
    [_labelTitle release];
    [_imgPhoto release];
    [_labelName release];
    [_labelTime release];
    [_imgComment release];
    [_labelComment release];
    [super dealloc];
}

#pragma mark - ------------ 初始化 ---------------

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;
}

#pragma mark - ------------ 创建控件 ---------------

-(void)create{
    
    //评论图标
    self.imgComment = [[baseImageView alloc]init];
    [self.contentView addSubview:self.imgComment];
    _imgComment.image =[UIImage imageNamed:@"pinglun"];
   
    //评论
    self.labelComment = [[baseLabel alloc]init];
    [self.imgComment addSubview:self.labelComment];
    self.labelComment.textAlignment = NSTextAlignmentCenter;
    self.labelComment.textColor = [UIColor grayColor];
    self.labelComment.font = [UIFont systemFontOfSize:13];
    
    //题目
    self.labelTitle = [[baseLabel alloc]init];
    [self.contentView addSubview:self.labelTitle];
    self.labelTitle.numberOfLines = 0;
    self.labelTitle.font = [UIFont systemFontOfSize:13];
    
    //图片
    self.imgPhoto = [[baseImageView alloc]init];
    [self.contentView addSubview:self.imgPhoto];
    //按比例填充
    _imgPhoto.contentMode = UIViewContentModeScaleAspectFill;
    _imgPhoto.clipsToBounds = YES;
    
    //名字
    self.labelName = [[baseLabel alloc]init];
    [self.contentView addSubview: self.labelName];
    self.labelName.textColor = [UIColor grayColor];
    self.labelName.font = [UIFont systemFontOfSize:13];
    
    //时间
    self.labelTime = [[baseLabel alloc]init];
    [self.contentView addSubview:self.labelTime];
    self.labelTime.textColor = [UIColor grayColor];
    self.labelTime.font = [UIFont systemFontOfSize:13];
    self.labelTime.textAlignment =NSTextAlignmentRight;
    
    
//    _imgPhoto.backgroundColor = [UIColor redColor];
//    _labelTime.backgroundColor = [UIColor yellowColor];
//    _labelComment.backgroundColor = [UIColor blueColor];
//    _labelName.backgroundColor = [UIColor blackColor];
//    _labelTitle.backgroundColor = [UIColor purpleColor];
//    _imgComment.backgroundColor = [UIColor greenColor];
    
    [_labelName release];
    [_imgPhoto release];
    [_labelTime release];
    [_labelTitle release];
    [_labelComment release];
    [_imgComment release];
    
}

#pragma mark - ------------ 布局 ---------------

-(void)layoutSubviews{
    
    [super layoutSubviews];
    //评论
    [self.imgComment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(10);
        make.top.mas_equalTo(self.contentView).offset(5);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(38);
    }];
    //更新
    [self layoutIfNeeded];
    //评论数
    _labelComment.frame = CGRectMake(0, -3, _imgComment.bounds.size.width, _imgComment.bounds.size.height);
     //标题 自适应
    CGFloat h1 = [[self class]heightWithText:_labelTitle.text];
    [_labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(8);
        make.left.mas_equalTo(_imgComment.mas_right).offset(10);
        make.height.mas_equalTo(h1);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    //判断有没有图片
    
    if (_model.path != nil) {
        //图片
        _imgPhoto.hidden =NO;
        [_imgPhoto mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.left.mas_equalTo(self.contentView).offset(55);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(120);
        }];
            //用户名
        [_labelName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_imgPhoto);
            make.top.mas_equalTo(_imgPhoto.mas_bottom).offset(10);
            make.right.mas_equalTo(_labelTime.mas_left).offset(-10);
            make.height.mas_equalTo(30);
        }];
        /** 时间 转换格式 */
        [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelName);
            make.right.mas_equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(100);
            
        }];
        
    }
    
    else{
    
        _imgPhoto.hidden = YES;
        
        /** 用户名 . club名 */
        [_labelName mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView).offset(55);
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.right.mas_equalTo(_labelTime.mas_left).offset(-10);
            make.height.mas_equalTo(30);
            
        }];
        
        /** 时间 转换格式 */
        [_labelTime mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(_labelTitle.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(60);
            
        }];
        
    }
}
#pragma mark - ------------ 赋值 ---------------

-(void)setModel:(TopicModel *)model{
   
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    //评论数
    _labelComment.text = model.comment_count.stringValue;
    if (_labelComment.text.integerValue > 1000) {
        _labelComment.text = [NSString stringWithFormat:@"%ldk",_labelComment.text.integerValue/1000];
    }
    
    //题目
    _labelTitle.text = model.content;
    
    _labelName.text = [[model.username stringByAppendingString:@" . "] stringByAppendingString:model.clubname];
    
    /** 时间戳转换 */
    _labelTime.text = [[self class]howlongToCurrntTime:model.active_time.stringValue];
    
    if (model.path!= nil) {
        
        [_imgPhoto sd_setImageWithURL:[NSURL URLWithString:model.path]];
    }
    
    
    


}






#pragma mark - ------------ 文本高度自适应 ---------------
+ (CGFloat)heightWithText:(NSString *)text
{
    
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(([UIScreen mainScreen].bounds.size.width) - 65, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.height;
    
}

#pragma mark - ------------ 时间转换 ---------------

+ (NSString *)howlongToCurrntTime:(NSString *)time
{
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time.doubleValue];
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc]init];
    [dateFomatter setDateFormat:@"HH:mm"];
    
    //1970  转至今的时间
    NSString *fomatterString = [dateFomatter stringFromDate:date];
    
    [dateFomatter release];
    
    return fomatterString;
    
    
}



@end
